package com.runa.rccprotocol.dto.debao;

import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.util.ByteUtil;

public class DeBaoReportAck extends DtuReportAck {

    public DeBaoReportAck(byte[] reportData) {
        this.data = new byte[]{(byte) 0x68, (byte) 0x32, (byte) 0x00, (byte) 0x32, (byte) 0x00,
                0x68, 0x49, 0, 0, 0, 0, 0x02,
                0x00, 0x60, 0x00, 0x00, 0x01, 0x00,
                0x00, 0x16};
        for (int i = 7; i < 12; i++) {
            data[i] = reportData[i];
        }
        data[13] = (byte)(reportData[13] & 0x6f);
        data[data.length-2] = ByteUtil.sum(data, 6, data.length-2-6);

    }

    public DeBaoReportAck(String netEquNo) {
        this.data = new byte[]{(byte) 0x68, (byte) 0x32, (byte) 0x00, (byte) 0x32, (byte) 0x00,
                0x68, 0x00, 0, 0, 0, 0, 0x02,
                0x00, 0x60, 0x00, 0x00, 0x01, 0x00,
                0x00, 0x16};
        ByteUtil.tenstringToBcd(data, netEquNo.substring(netEquNo.length() - 4), 7, false);
        String format = String.format("%x", Short.parseShort(netEquNo.substring(0, 5)));
        data[9] = ByteUtil.hexStringToByte(format.substring(2,4))[0];
        data[10] = ByteUtil.hexStringToByte(format.substring(0,2))[0];
//        char[] chars = format.toCharArray();
//        data[9] = (byte) ((chars[2] - 0x30<<4) | (chars[3] - 0x30)&0x0f);
//        data[10] = (byte) ((chars[0] - 0x30<<4) | (chars[1] - 0x30)&0x0f);
//        ByteUtil.tenstringToBcd(data, format, 9, false);
        data[data.length-2] = ByteUtil.sum(data, 6, data.length-2-6);
    }

    public static void main(String[] args) {
        DeBaoReportAck deBaoReportAck = new DeBaoReportAck("100011001");
        System.out.println(ByteUtil.byteToHexString(deBaoReportAck.getData()));
    }
}
